package com.cardbaobao.cardbabyclient.ui.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.CreditCardIndexLayout;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Description:
 * Created by FHW
 * on 2017/6/13
 */

public class HighPrivilegeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final int TYPE_CONTENT=1;
    private static final int TYPE_FOOTER=2;
    private int radian;
    private List<CreditCardIndexLayout.HighPrivilege> data;
    public HighPrivilegeAdapter(List<CreditCardIndexLayout.HighPrivilege> data){
        this.data=data;
        this.radian=DensityUtil.dp2px(6.0f);
    }
    private RecycleItemClickedCallback clickedCallback;
    public void setClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        if(viewType==TYPE_CONTENT){
            return new ContentHolder(inflater.inflate(R.layout.item_high_privilege_content,parent,false));
        }else if(viewType==TYPE_FOOTER){
            return new FooterHolder(inflater.inflate(R.layout.item_high_privilege_footer,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        int viewType=getItemViewType(position);
        if(viewType==TYPE_CONTENT){
            ContentHolder contentHolder= (ContentHolder) holder;
            if(position==0){
                ((ViewGroup.MarginLayoutParams)holder.itemView.getLayoutParams()).topMargin=
                DensityUtil.dp2px(5);
            }
            switch (position){
                case 0:
                    contentHolder.ivLevel.setImageResource(R.mipmap.icon_gold_medal);
                    contentHolder.ivLevel.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    contentHolder.ivLevel.setImageResource(R.mipmap.icon_silver_medal);
                    contentHolder.ivLevel.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    contentHolder.ivLevel.setImageResource(R.mipmap.icon_bronze_medal);
                    contentHolder.ivLevel.setVisibility(View.VISIBLE);
                    break;
                default:
                    contentHolder.ivLevel.setVisibility(View.GONE);
                    break;
            }
            CreditCardIndexLayout.HighPrivilege privilege=data.get(position);
            contentHolder.tvTitle.setText(privilege.getName());
            contentHolder.tvDiscountTop.setText(privilege.getMarketing_one());
            contentHolder.tvDiscountBottom.setText(privilege.getMarketing_two());
            contentHolder.tvApplyCount.setText(privilege.getApply_count());
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+privilege.getPicture_path(),
                    contentHolder.ivPic,
                    R.mipmap.img_default_rectangle,
                    radian);
            contentHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onRecycleItemClicked(holder.getAdapterPosition());
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return (position==getItemCount()-1)?TYPE_FOOTER:TYPE_CONTENT;
    }


    private class ContentHolder extends RecyclerView.ViewHolder{
        private ImageView ivPic;
        private ImageView ivLevel;
        private TextView tvTitle;
        private TextView tvDiscountTop;
        private TextView tvDiscountBottom;
        private TextView tvApplyCount;
        private ContentHolder(View itemView) {
            super(itemView);
            ivPic= (ImageView) itemView.findViewById(R.id.iv_pic);
            ivLevel= (ImageView) itemView.findViewById(R.id.iv_level);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            tvDiscountTop= (TextView) itemView.findViewById(R.id.tv_discount_top);
            tvDiscountBottom= (TextView) itemView.findViewById(R.id.tv_discount_bottom);
            tvApplyCount= (TextView) itemView.findViewById(R.id.tv_apply_count);
        }
    }

    private class FooterHolder extends RecyclerView.ViewHolder{
        private FooterHolder(View itemView) {
            super(itemView);
        }
    }
}
